package com.net.xpay.common.service.impl;

import com.google.common.base.Strings;
import com.net.xpay.common.domain.CheckCode;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.operator.OperatorUserBindLog;
import com.net.xpay.common.domain.operator.OperatorUserBinding;
import com.net.xpay.common.enums.operator.OperatorUserBindLogType;
import com.net.xpay.common.enums.user.CheckCodeType;
import com.net.xpay.common.manager.CheckCodeManager;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.model.AddOperatorUserModel;
import com.net.xpay.common.validator.CheckCodeValidator;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.common.exception.BusinessException;
import com.net.common.util.PhoneUtil;
import com.net.xpay.common.manager.operator.OperatorUserBindLogManager;
import com.net.xpay.common.manager.operator.OperatorUserBindingManager;
import com.net.xpay.common.service.OperatorUserBindingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OperatorUserBindingServiceImpl implements OperatorUserBindingService {

    @Autowired
    private OperatorUserBindingManager operatorUserBindingManager;

    @Autowired
    private UserManager userManager;

    @Autowired
    private CheckCodeManager checkCodeManager;

    @Autowired
    private CheckCodeValidator checkCodeValidator;

    @Autowired
    private OperatorUserBindLogManager operatorUserBindLogManager;

    @Override
    public void addBindingCompany(Long operatorUserId, AddOperatorUserModel model) {

        if (Strings.isNullOrEmpty(model.getMobile())){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号不能为空");
        }
        //验证手机号
        if (!PhoneUtil.isEffectiveMobile(model.getMobile())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号格式不正确");
        }
        if (Strings.isNullOrEmpty(model.getCheckCode())){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "验证码不能为空");
        }

        //验证验证码
        CheckCode checkCode = checkCodeManager.findByMobileAndType(model.getMobile(), CheckCodeType.OPERATOR_ADD_BINDING_USER);
        checkCodeValidator.validateCheckCode(checkCode, model.getCheckCode());

        User user = userManager.findByMobile(model.getMobile());
        if(user == null){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "公司不存在");
        }
        OperatorUserBinding userBinding = operatorUserBindingManager.findByUserId(user.getId());
        if (userBinding != null){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "公司已被关联");
        }
        OperatorUserBinding operatorUserBinding = operatorUserBindingManager.findByOperatorUserIdAndUserId(operatorUserId, user.getId());
        if(operatorUserBinding != null){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "公司已添加,请勿重复添加");
        }

        OperatorUserBinding binding = new OperatorUserBinding();
        binding.setOperatorUserId(operatorUserId);
        binding.setUserId(user.getId());
        operatorUserBindingManager.save(binding);

        OperatorUserBindLog bindLog = new OperatorUserBindLog();
        bindLog.setType(OperatorUserBindLogType.ADD);
        bindLog.setUserId(user.getId());
        bindLog.setOperatorUserId(operatorUserId);
        bindLog.setUserName(user.getName());
        operatorUserBindLogManager.save(bindLog);
    }
}
